package us.icitap.commandcenter.ui.dispatcher;

import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.business.ejb.persistence.UniqueNumber;
import us.icitap.commandcenter.listeners.ManageCallListener;
import us.icitap.commandcenter.ui.custom.ButtonsLayout;
import us.icitap.commandcenter.ui.custom.CustButton;
import us.icitap.commandcenter.utils.HandleNotification;
import us.icitap.utils.Result;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class UniqueNumberWindow extends Window{

	private final CustButton createButton = new CustButton("Krijo numer unik");

	private final UniqueNumberContent unContent = new UniqueNumberContent();
	
	private Call call;
	
	public UniqueNumberWindow(final ManageCallListener manageCallListener,  Call call) {

		this.setModal(true);
		this.setResizable(false);
		this.center();
		
		this.call = call;
		this.setCaption("Krijimi i numrit unik");
		
		final VerticalLayout vl = new VerticalLayout();
		vl.setMargin(false);
		vl.setSpacing(true);
		
		vl.addComponent(unContent);

		final ButtonsLayout buttonsLayout = new ButtonsLayout("530px");
		buttonsLayout.addComponent(createButton);
		buttonsLayout.setComponentAlignment(createButton, Alignment.MIDDLE_RIGHT);
		vl.addComponent(buttonsLayout);
				
		this.setContent(vl);
		
		this.createButton.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				Result<UniqueNumber> result = unContent.createUniqueNumberObject();
				if(result.isSucceeded()){
					UniqueNumberWindow.this.call.setUniqueNumber(result.getObject());
					manageCallListener.generateUniqueNumber(UniqueNumberWindow.this.call);
				}
				else
					HandleNotification.showWarningOnScreen(result.getMessage());
				
				
				
			}
		});
		
		
		
	}

	public void setCall(Call call) {
		unContent.resetContent();
		this.call = call;
		
	}
	
}
